package indi.qing.service.impl;
import java.util.Date;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import indi.qing.VO.ProcessVO;
import indi.qing.VO.RelationVO;
import indi.qing.entity.Relation;
import indi.qing.entity.User;
import indi.qing.mapper.RelationMapper;
import indi.qing.mapper.UserMapper;
import indi.qing.service.RelationService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 熊卿智
 * @since 2022-01-17
 */
@Service
public class RelationServiceImpl extends ServiceImpl<RelationMapper, Relation> implements RelationService {

    @Resource
    private RelationMapper relationMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public void addFriend(Relation relation) {
        User user = userMapper.selectById(relation.getUser2Id());
        if (ObjectUtils.isEmpty(user)) {
            throw new RuntimeException("申请添加的用户不存在");
        }
        List<Relation> relations = lambdaQuery()
                .eq(Relation::getUser1Id, relation.getUser1Id())
                .eq(Relation::getUser2Id, relation.getUser2Id())
                .list();
        if (!CollectionUtils.isEmpty(relations)) {
            Relation relation1 = relations.get(0);
            Integer state = relation1.getState();
            if (state == 0 || state == 2) {
                lambdaUpdate()
                        .set(Relation::getState, 0)
                        .set(Relation::getName,relation.getName())
                        .set(Relation::getRelationType,relation.getRelationType())
                        .eq(Relation::getRelationId, relation1.getRelationId());
                return;
            } else if (state == 1) {
//                List<Relation> relationList = lambdaQuery()
//                        .eq(Relation::getUser1Id, relation.getUser2Id())
//                        .eq(Relation::getUser2Id, relation.getUser1Id())
//                        .list();
                throw new RuntimeException("你们已经是好友");
            }
        }

        relation.setState(0);
        int insert = relationMapper.insert(relation);
        if (insert < 1) {
            throw new RuntimeException("申请添加失败");
        }
    }

    @Override
    public void deleteFriend(Long user1Id, Long user2Id) {
        User user = userMapper.selectById(user2Id);
        if (ObjectUtils.isEmpty(user)) {
            throw new RuntimeException("删除的用户不存在");
        }
        List<Relation> relations = lambdaQuery()
                .eq(Relation::getUser1Id, user1Id)
                .eq(Relation::getUser2Id, user2Id)
                .list();
        if (CollectionUtils.isEmpty(relations)) {
            throw new RuntimeException("该用户不是您的好友");
        }
        Relation relation = relations.get(0);
        int delete = relationMapper.deleteById(relation.getRelationId());
        if (delete < 1) {
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public List<RelationVO> getAllAdd(Long userId) {
        List<RelationVO> res = new ArrayList<>();
        List<Relation> relations = lambdaQuery()
                .eq(Relation::getUser2Id, userId)
                .eq(Relation::getState, 0)
                .orderByDesc(Relation::getUpdateTime)
                .list();
        if(!CollectionUtils.isEmpty(relations)){
            relations.stream().forEach(relation -> {
                Long user1Id = relation.getUser1Id();
                User user = userMapper.selectById(user1Id);
                RelationVO relationVO = new RelationVO();
                relationVO.setRelationId(relation.getRelationId());
                relationVO.setUser1Id(relation.getUser1Id());
                relationVO.setName(user.getUsername());
                relationVO.setState(relation.getState());
                relationVO.setAddTime(relation.getUpdateTime());
                res.add(relationVO);
            });
        }
        return res;
    }

    @Override
    public void processAdd(ProcessVO processVO) {
        Relation relation = relationMapper.selectById(processVO.getRelationId());
        Integer state = processVO.getState();
        relation.setState(state);
        int update = relationMapper.updateById(relation);
        if(update<1){
            throw new RuntimeException("操作失败");
        }
        if (state == 1){
            List<Relation> relations = lambdaQuery()
                    .eq(Relation::getUser1Id, relation.getUser2Id())
                    .eq(Relation::getUser2Id, relation.getUser1Id())
                    .list();
            if(CollectionUtils.isEmpty(relations)){
                Relation relation1 = new Relation();
                relation1.setUser1Id(relation.getUser2Id());
                relation1.setUser2Id(relation.getUser1Id());
                relation1.setRelationType(processVO.getRelationType());
                relation1.setName(processVO.getName());
                relation1.setState(1);
                int insert = relationMapper.insert(relation1);
                if(insert<1) {
                    throw new RuntimeException("操作失败");
                }
                return;
            }
            Relation relation1 = relations.get(0);
            relation1.setRelationType(processVO.getRelationType());
            relation1.setName(processVO.getName());
            relation1.setState(1);
            int update1 = relationMapper.updateById(relation1);
            if(update1 < 1){
                throw new RuntimeException("操作失败");
            }
        }

    }

    @Override
    public List<Relation> getFriend(Long userId) {
        return lambdaQuery()
                .eq(Relation::getUser1Id,userId)
                .eq(Relation::getState,1)
                .list();
    }
}
